//
//  WDGrabOrdersPersonInfoTopView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/13.
//

#import "WDGrabOrdersPersonInfoTopView.h"
#import "WDArrivalServiceItemModel.h"

@interface WDGrabOrdersPersonInfoTopView ()
@property (nonatomic, strong) WDArrivalServiceItemModel *model;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) CALayer *topLine;

/// 车牌号
@property (nonatomic, strong) UILabel *plateNumberLabel;

/// vin码后八位
@property (nonatomic, strong) UILabel *vinLabel;

/// 更新时间
@property (nonatomic, strong) UILabel *updateTimeLabel;

/// 车辆品牌图片
@property (nonatomic, strong) UIImageView *mainImgView;

/// 车系 车型
@property (nonatomic, strong) UILabel *typeLabel;

/// 工单号
@property (nonatomic, strong) UILabel *workOrderCodeLabel;

/// 预约时间
@property (nonatomic, strong) UILabel *appointDatetimeLabel;

/// 具体李服务站的距离
@property (nonatomic, strong) UILabel *distanceLabel;

@property (nonatomic, strong) UIImageView *locImgView;

@property (nonatomic, strong) UILabel *adsLabel;


@end

@implementation WDGrabOrdersPersonInfoTopView

- (void)initWithSubView {
    [super initWithSubView];
    
//    self.backgroundColor = [UIColor systemPinkColor];
    
    [self initWithBgView];
    
    [self initWithTopLine];
}

- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(0, 0, self.width, kWDGrabOrdersPersonInfoTopViewHeight - 0);
        _bgView = [[UIView alloc] initWithFrame:frame];
//        _bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_bgView];
    }
}

- (void)initWithTopLine {
    if (!_topLine) {
        CGRect frame = CGRectMake(0, 30, self.width, 1);
        _topLine = [CALayer drawLine:self.bgView frame:frame lineColor:[UIColor colorWithHexString:@"#999999"]];
    }
}

/// 车牌号
- (void)initWithPlateNumberLabel {
    if (!_plateNumberLabel) {
        NSString *str = @"皖A12345645";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        float width = [str widthForFont:font];
        CGRect frame = CGRectMake(16, self.topLine.bottom - 18, width, 16);
        _plateNumberLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
    }
    if (self.model) {
        self.plateNumberLabel.text = objectNull(self.model.plateNumber);
    }
}

- (void)initWithVinLabel {
    if (!_vinLabel) {
        CGRect frame = _plateNumberLabel.frame;
        frame.size.width = frame.size.width + 10;
        frame.origin.x = _plateNumberLabel.right + 5;
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _vinLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
    }
    if (self.model) {
        if (self.model.vehicleVin && self.model.vehicleVin.length >= 8) {
            self.vinLabel.text = [self.model.vehicleVin substringFromIndex:self.model.vehicleVin.length - 8];
        } else {
            self.vinLabel.text = objectNull(self.model.vehicleVin);
        }
    }
}

/// 更新时间
- (void)initWithUpdateTimeLabel {
    if (!_updateTimeLabel) {
        NSString *str = @"更新：2024-12-12 23:34:54";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        float width = [str widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.width - 16 - width, self.topLine.bottom - 18, width, 16);
        _updateTimeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textAlignment = NSTextAlignmentRight;
        }];
    }
    if (self.model) {
        self.updateTimeLabel.text = [NSString stringWithFormat:@"更新：%@", objectNull(self.model.updateDatetime)];;
    }
}

- (void)initWithMainImgView {
    if (!_mainImgView) {
        CGRect frame = CGRectMake(16, _topLine.bottom + 12, 90, 70);
        _mainImgView = [[UIImageView alloc] initWithFrame:frame];
//        _mainImgView.backgroundColor = [UIColor orangeColor];
        [self addSubview:_mainImgView];
    }
    if (self.model) {
        [_mainImgView sd_setImageWithURL:[NSURL URLWithString:self.model.imgPath] placeholderImage:nil];
    }
}

/// 车系 车型
- (void)initWithTypeLabel {
    if (!_typeLabel) {
//        NSString *str = @"车系 ·  车型";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
//        float width = [str widthForFont:font] + 5;
        CGRect frame = CGRectMake(self.mainImgView.right + 8, self.mainImgView.top, SCREEN_WIDTH - self.mainImgView.right - 8 - 16, 16);
        _typeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
    }
    if (self.model) {
        self.typeLabel.text = [NSString stringWithFormat:@"%@ · %@", self.model.modelName, self.model.vehicleSalesValue];
    }
}

/// 工单号
- (void)initWithWorkOrderCodeLabel {
    if (!_workOrderCodeLabel) {
        NSString *str = @"工单号：ABCDQWEQ";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        float width = [str widthForFont:font] + 5;
        CGRect frame = CGRectMake(self.mainImgView.right + 8, self.mainImgView.top, width, 16);
        _workOrderCodeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
        _workOrderCodeLabel.centerY = self.mainImgView.centerY;
    }
    if (self.model) {
        self.workOrderCodeLabel.text = [NSString stringWithFormat:@"工单号：%@", self.model.taskNo];
    }
}

/// 预约时间
- (void)initWithAppointDatetimeLabel {
    if (!_appointDatetimeLabel) {
        NSString *str = @"预约日期：2024-05-12 12:34";
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        float width = [str widthForFont:font] + 5;
        CGRect frame = CGRectMake(self.mainImgView.right + 8, self.mainImgView.bottom - 16, width, 16);
        _appointDatetimeLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
//            label.text = str;
        }];
    }
    if (self.model) {
        self.appointDatetimeLabel.text = [NSString stringWithFormat:@"预约日期：%@", self.model.appointmentTime];
    }
}

/// 距离服务站的距离
- (void)initWithDistanceLabel {
    if (!_distanceLabel) {
        CGRect frame = self.appointDatetimeLabel.frame;
        frame.origin.y = self.appointDatetimeLabel.bottom + 8;
        frame.size.width = self.width - self.appointDatetimeLabel.left - 16;
        UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _distanceLabel = [UILabel createWithLabel:self frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"距离服务站：150km";
        }];
    }
}

- (void)initWithLocImgView {
    if (!_locImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_loc_ads"];
        CGRect frame = CGRectMake(_mainImgView.left + 5, _distanceLabel.bottom + 10, image.size.width, image.size.height);
        _locImgView = [UIImageView createWithImageView:self.bgView frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            
        }];
    }
}

- (void)initWithAdsLabel {
    if (!_adsLabel) {
        CGRect frame = CGRectMake(self.locImgView.right + 8, 0, self.bgView.width - self.locImgView.right - 8 - 16, 20);
        _adsLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.text = @"安徽省合肥市望江西路与枫林路交口南150M";
        }];
    }
    _adsLabel.centerY = self.locImgView.centerY;
}


- (void)updateWithViewData:(id)data {
    self.model = data;
    
    [self initWithPlateNumberLabel];
    
    [self initWithVinLabel];

    [self initWithUpdateTimeLabel];

    [self initWithMainImgView];
    
    /// 车系 车型
    [self initWithTypeLabel];

    /// 工单号
    [self initWithWorkOrderCodeLabel];

    /// 预约时间
    [self initWithAppointDatetimeLabel];
    
    /// 距离服务站的距离
    [self initWithDistanceLabel];
    
    [self initWithLocImgView];
    
    [self initWithAdsLabel];
}

@end
